/**
  ******************************************************************************
  * @file     SD93F115B_saradc.h 						    	
  * @author   TEST TEAM 												    	
  * @version  v0															    	
  * @date     Septemper 2022												    	
  * @brief
  *
  * Copyright (C) 2022  Hangzhou SDIC Microelectronics Co., Ltd
*********************************************************************************/

#ifndef __SD93F115B_SARADC_H
#define __SD93F115B_SARADC_H

#ifdef __cplusplus
 extern "C" {
#endif

/********************* Includes**************************/
#include "SD93F115B.h"

/******************************SARADC CALIBRATION CONFIGURATION***********************************/

typedef enum
{
	SARADC_CalSampleTime_16SARCLK=0x00,
	SARADC_CalSampleTime_8SARCLK=0x01,
	SARADC_CalSampleTime_4SARCLK=0x02,
	SARADC_CalSampleTime_2SARCLK=0x03

}SARADCCalSampleTime_TypeDef;

#define IS_SARADC_CalSampleTime(Time) (((Time) ==SARADC_CalSampleTime_16SARCLK)||((Time) == SARADC_CalSampleTime_8SARCLK)||\
									   ((Time) ==SARADC_CalSampleTime_4SARCLK)||((Time) == SARADC_CalSampleTime_2SARCLK))

/******************************Buffer Configuration***********************************/
typedef enum
{
	SARADC_BufMode_LowSpeed=0x00,
	
	SARADC_BufMode_HighSpeed=0x01

}SARADCBufMode_TypeDef;

#define IS_SARADC_BufMode(MODE) (((MODE) ==SARADC_BufMode_LowSpeed)||((MODE) == SARADC_BufMode_HighSpeed)) 

typedef enum
{
	SARADC_BufPhase_N=0x00,
	
	SARADC_BufPhase_P=0x01

}SARADCBufPhase_TypeDef;

#define IS_SARADC_BufPhase(Phase) (((Phase) ==SARADC_BufPhase_N)||((Phase) == SARADC_BufPhase_P)) 

/************************************SARADC Configuration*****************************************/

typedef enum
{
	SARADC_ReadyTime_128SARCLK = 0,
    SARADC_ReadyTime_64SARCLK,
	SARADC_ReadyTime_32SARCLK,
	SARADC_ReadyTime_16SARCLK

}SARADCReadyTime_TypeDef;

#define IS_SARADC_ReadyTime(Time) (((Time) ==SARADC_ReadyTime_128SARCLK)||((Time) == SARADC_ReadyTime_64SARCLK)||\
								   ((Time) ==SARADC_SARADC_ReadyTime_32SARCLK)||((Time) == SARADC_ReadyTime_16SARCLK))

typedef enum
{
	SARADC_SampleTime_4SARCLK = 0,
    SARADC_SampleTime_8SARCLK,
	SARADC_SampleTime_16SARCLK,
	SARADC_SampleTime_32SARCLK,
	SARADC_SampleTime_64SARCLK,
    SARADC_SampleTime_128SARCLK,
	SARADC_SampleTime_256SARCLK,
	SARADC_SampleTime_512SARCLK

}SARADCSampleTime_TypeDef;

#define IS_SARADC_SampleTime(Time) (((Time) ==SARADC_SampleTime_4SARCLK)||((Time) == SARADC_SampleTime_8SARCLK)||\
									((Time) ==SARADC_SampleTime_16SARCLK)||((Time) == SARADC_SampleTime_32SARCLK)||\
									((Time) ==SARADC_SampleTime_64SARCLK)||((Time) == SARADC_SampleTime_128SARCLK)||\
									((Time) ==SARADC_SampleTime_256SARCLK)||((Time) == SARADC_SampleTime_512SARCLK))
typedef enum
{
	SARADC_SARCLK_IHRC_DIV96 = 0,
    SARADC_SARCLK_IHRC_DIV48,
	SARADC_SARCLK_IHRC_DIV24,
	SARADC_SARCLK_IHRC_DIV12,
	SARADC_SARCLK_IHRC_DIV6,
    SARADC_SARCLK_IHRC_DIV4,
	SARADC_SARCLK_IHRC_DIV2,
	SARADC_SARCLK_IHRC_DIV1

}SARADCCLK_TypeDef;

#define IS_SARADC_CLK(CLK) (((CLK) ==SARADC_SARCLK_IHRC_DIV96)||((CLK) == SARADC_SARCLK_IHRC_DIV48)||\
							((CLK) ==SARADC_SARCLK_IHRC_DIV24)||((CLK) == SARADC_SARCLK_IHRC_DIV12)||\
							((CLK) ==SARADC_SARCLK_IHRC_DIV6)||((CLK) == SARADC_SARCLK_IHRC_DIV4)||\
							((CLK) ==SARADC_SARCLK_IHRC_DIV4=2)||((CLK) == SARADC_SARCLK_IHRC_DIV1))
   
typedef struct
{
	SARADCBufMode_TypeDef SARADC_BufMode;       //SAR Buf高低速模式及电流选择位
	SARADCBufPhase_TypeDef SARADC_BufPhase;     //SAR Buf相位选择位
	FunctionalState SARADC_BufEN;               //SAR Buf使能位
	uint8_t SARADC_Channel;                     //SARADC输入信号选择位
	
	SARADCCLK_TypeDef SARADC_SARCLK;            //SAR ADC采样时钟SAR_CLK选择位
	SARADCReadyTime_TypeDef SARADC_ReadyTime;   //SAR ADC准备时间选择位
	SARADCSampleTime_TypeDef SARADC_SampleTime; //SAR ADC转换时信号采样周期数选择位
	FunctionalState SARADC_CompCap;             //比较器电容选择使能
    FunctionalState SARADC_ContinuousMode;      //转换模式选择位
    FunctionalState SARADC_WaitMode;	        //wait模式使能位
	FunctionalState SARADC_OVR_LoseCurrentValue;//发生OVR错误时是否保留当前数据	
	FunctionalState SARADC_HighSpeedMode;	    //SAR ADC高低速模式及电流选择位

}SARADC_InitTypeDef;


/************************SARADC_SARIN_Channel******************************/
#define SARADC_Channel_A0									((uint8_t)0x00)		
#define SARADC_Channel_A1            						((uint8_t)0x04) 
#define SARADC_Channel_A2									((uint8_t)0x08)		
#define SARADC_Channel_A3            						((uint8_t)0x0C)
#define SARADC_Channel_A4									((uint8_t)0x10)		
#define SARADC_Channel_A5            						((uint8_t)0x14) 
#define SARADC_Channel_A6									((uint8_t)0x18)		
#define SARADC_Channel_A7            						((uint8_t)0x1C) 
#define SARADC_Channel_DAO								    ((uint8_t)0x20)		
#define SARADC_Channel_P04            					    ((uint8_t)0x24) 
#define SARADC_Channel_P03								    ((uint8_t)0x28)		
#define SARADC_Channel_P02            					    ((uint8_t)0x2C) 
#define SARADC_Channel_IAOUTP								((uint8_t)0x30)		
#define SARADC_Channel_IAOUTN            					((uint8_t)0x34) 
#define SARADC_Channel_VSS								    ((uint8_t)0x38)		
#define SARADC_Channel_AVDDR            					((uint8_t)0x3C) 
  
#define IS_SARADC_Channel(Channel)(((Channel)==SARADC_Channel_A0)||((Channel)==SARADC_Channel_A1)||\
								   ((Channel)==SARADC_Channel_A2)||((Channel)==SARADC_Channel_A7)||\
								   ((Channel)==SARADC_Channel_A3)||((Channel)==SARADC_Channel_DAO)||\
								   ((Channel)==SARADC_Channel_A4)||((Channel)==SARADC_Channel_P04)||\
								   ((Channel)==SARADC_Channel_A5)||((Channel)==SARADC_Channel_P03)||\
								   ((Channel)==SARADC_Channel_A6)||((Channel)==SARADC_Channel_P02)||\
								   ((Channel)==SARADC_Channel_IAOUTP)||((Channel)==SARADC_Channel_VSS)||\
								   ((Channel)==SARADC_Channel_IAOUTN)||((Channel)==SARADC_Channel_AVDDR))


/*************************SARADC_IT************************************/
#define SARADC_CONIE							SARADC_CR5_CONIE 		
#define SARADC_RDYIE             				SARADC_CR5_RDYIE 
#define SARADC_OVRIE          					SARADC_CR5_OVRIE 
#define SARADC_SAMPIE          					SARADC_CR5_SAMPIE

#define IS_SARADC_ITSource(IT) (((IT)==SARADC_CONIE)||((IT)==SARADC_RDYIE)||\
								((IT)==SARADC_OVRIE)||((IT)==SARADC_SAMPIE))

/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
/* Initialization and Configuration functions *********************************/
void SARADC_DeInit(void);                                                                                     //SARADC相关寄存器重设为上电默认值

void SARADC_Cmd(FunctionalState NewState);                                                                    //使能SARADC功能模块

void SARADC_Init(SARADC_InitTypeDef* SARADC_InitStruct);				                                      //SARADC初始化
void SARADC_ConversionCmd(FunctionalState NewState);                                                          //使能SARADC转换
void SARADC_StopConversionCmd(FunctionalState NewState);                                                      //使能停止SARADC转换

void SARADC_StructInit(SARADC_InitTypeDef* SARADC_InitStruct);				                                  //初始化成常规应用？？？？？？？？？？？？？？？？待测试结果出来后，配置一个理想的应用设置

void SARADC_ChannelConfig(uint8_t SARADC_Channel);                                                            //配置SARADC信号输入通道 
void SARADC_SARCLKConfig(SARADCCLK_TypeDef SARADC_SARCLK);                                                    //配置SARADC的采样时钟
void SARADC_ReadyTimeConfig(SARADCReadyTime_TypeDef SARADC_ReadyTime);                                        //配置SARADC的准备时间
void SARADC_SampleTimeConfig(SARADCSampleTime_TypeDef SARADC_SampleTime);                                     //配置SARADC转换时的采样周期

void SARADC_BufferConfig(SARADCBufMode_TypeDef SARADC_BufMode,SARADCBufPhase_TypeDef SARADC_BufPhase);	      //SARADC的BUFFER初始化
void SARADC_BufferCmd(FunctionalState NewState);				                                              //SARADC_Buffer使能

void SARADC_CompCapCmd(FunctionalState NewState);                                                             //比较器电容选择使能
void SARADC_ContinuousModeCmd(FunctionalState NewState);                                                      //选择SARADC的转换模式，单次或连续
void SARADC_WaitModeCmd(FunctionalState NewState);	                                                          //使能SARADC工作于WAIT模式
void SARADC_OVR_LoseCurrentValueCmd(FunctionalState NewState);	                                              //使能当SARADC出现溢出错误时，是否舍弃当前转换结果
void SARADC_HighSpeedModeCmd(FunctionalState NewState);                                                       //使能SARADC工作于高速模式

void SARADC_ITConfig(uint8_t SARADC_IT, FunctionalState NewState);          //使能SARADC相关的中断
ITStatus SARADC_GetITStatus(uint8_t SARADC_IT);                             //获取SARADC相关中断的标志位
void SARADC_ClearITStatus(uint8_t SARADC_IT);                               //清除SARADC相关中断的标志位

uint16_t SARADC_GetConversionValue(void);						            //获取SARADC转换结果

/******************************************************/
void SARADC_Change_Init(SARADC_InitTypeDef* SARADC_InitStruct);				//SARADC转换初始化
void SARADC_Buffer_Init(SARADC_InitTypeDef* SARADC_InitStruct);				//BUFFER初始化

#ifdef __cplusplus
}
#endif

#endif /* __SD93F115B_SARADC_H */

/**********************************END OF FILE*********************************/